home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
src.arc
/
TNSERV.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-08-18
|
3KB
|
121 lines
#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "socket.h"
#include "telnet.h"
#include "session.h"
#include "proc.h"
#include "tty.h"
#include "commands.h"
static int Stelnet = -1; /* Protoype socket for service */
static void tnhandle __ARGS((int s,void *unused,void *p));
int
tnstart(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct sockaddr_in lsocket;
int s;
if(Stelnet != -1){
freeargs(argc,argv);
return 0;
}
psignal(Curproc,0); /* Don't keep the parser waiting */
chname(Curproc,"Telnet listener");
lsocket.sin_family = AF_INET;
lsocket.sin_addr.s_addr = Ip_addr;
if(argc < 2)
lsocket.sin_port = IPPORT_TELNET;
else
lsocket.sin_port = atoi(argv[1]);
freeargs(argc,argv);
Stelnet = socket(AF_INET,SOCK_STREAM,0);
bind(Stelnet,(char *)&lsocket,sizeof(lsocket));
listen(Stelnet,1);
for(;;){
if((s = accept(Stelnet,NULLCHAR,(int *)NULL)) == -1)
break; /* Service is shutting down */
newproc("in_telnet",2048,tnhandle,s,NULL,NULL);
}
return 0;
}
/* Handle incoming Telnet session */
static void
tnhandle(s,unused,p)
int s;
void *unused;
void *p;
{
struct session *sp;
char *cp;
struct sockaddr_in fsocket;
struct mbuf *bp;
int i;
struct telnet tn;
sockowner(s,Curproc); /* We own it now */
log(s,"open Telnet");
/* Allocate a session descriptor */
if((sp = newsession(NULLCHAR,TELNET)) == NULLSESSION){
usprintf(s,"Too many sessions\n");
close_s(s);
return;
}
sp->ttymode = TTY_EDIT|TTY_ECHO;
/* Initialize a Telnet protocol descriptor */
memset((char *)&tn,0,sizeof(tn));
tn.session = sp; /* Upward pointer */
sp->cb.telnet = &tn; /* Downward pointer */
sp->s = s;
tn.output = Curproc;
i = SOCKSIZE;
getpeername(s,(char *)&fsocket,&i);
cp = psocket((struct sockaddr *)&fsocket);
printf("\007Incoming telnet session %u from %s\007\n",
(unsigned)(sp - Sessions),cp);
/* Fork off the receiver process */
tn.input = newproc("tel_in",512,tel_input,0,&tn,NULL);
/* Now send whatever's typed on the terminal */
for(;;){
while(sp->input == NULLBUF)
if(pwait(&sp->input) == -1)
goto quit;
bp = dequeue(&sp->input);
/* If we're doing our own echoing and recording is enabled,
* record it
*/
if(tn.remote[TN_ECHO] && sp->record != NULLFILE)
write_p(sp->record,bp);
if(send_mbuf(s,bp,0,NULLCHAR,0) == -1)
break;
}
quit: close_s(s);
killproc(tn.input);
freesession(sp);
}
/* Shut down Telnet server */
int
tn0(argc,argv,p)
int argc;
char *argv[];
void *p;
{
close_s(Stelnet);
Stelnet = -1;
return 0;
}